package cn.gzsxt.music.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.gzsxt.music.service.AdminService;
import cn.gzsxt.music.service.RoleService;
import cn.gzsxt.music.util.ConstantUtil;
import cn.gzsxt.music.vo.MsgObj;

@Controller
@Scope("request")
@RequestMapping("/index")
public class IndexController {
	
	@Autowired
	private AdminService service;
	
	//引入角色Service层
	@Autowired
	private RoleService roleService;
	
	@RequestMapping(value="/login")
	@ResponseBody //返回字符串,使用jackson返回 登录结果即可
	public MsgObj login(@RequestParam Map<String,Object> params,HttpSession session) {
		System.out.println(params);
		
		Map<String,Object> admin  = service.login(params);
		System.out.println(admin);
		
		MsgObj msgObj = new MsgObj(0,"账号或者密码错误");
		//登录成功
		if(admin !=null) {
			//将用户信息共享到Session中
			session.setAttribute(ConstantUtil.USER_IN_SESSION, admin);
			//获取用户的角色信息(角色信息中包含用户拥有的权限)
			Map<String,Object> role =  roleService.selectRoleByRoleId((Long)admin.get("role_id"));
			//将角色信息保存的session中
			session.setAttribute(ConstantUtil.ROLE_IN_SESSION,role);
			session.setAttribute("admin_name", admin.get("admin_name"));
			session.setAttribute("admin_role", role.get("role_name"));
			session.setAttribute("create_date", admin.get("create_date"));
			session.setAttribute("admin_status", admin.get("admin_status"));
			System.out.println(admin);
			System.out.println(role);
			
			msgObj = new MsgObj(1, "登录成功");
		}
		return msgObj;
	}
	
	@RequestMapping("/exit")
	public String exit(HttpSession session) {
		session.invalidate();
		return "redirect:/login.jsp";
	}
	
	@RequestMapping("/home")
	public String index() {
		//跳转到WEB-INF/views/index.jsp首页面
		return "forward:/WEB-INF/views/index.jsp";
	}
	
	
}
